package com.qkl.xiatang.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qkl.xiatang.app.dto.feed.FeedVO;
import com.qkl.xiatang.app.dto.message.FeedMsgDTO;
import com.qkl.xiatang.app.dto.message.FeedMsgQueryDTO;
import com.qkl.xiatang.app.dto.pond.QueryPondNamesOutDTO;
import com.qkl.xiatang.app.entity.message.MessageDetailEntity;
import com.qkl.xiatang.app.mapper.MessageDetailMapper;
import com.qkl.xiatang.app.mapper.MessageMapper;
import com.qkl.xiatang.app.utils.AppUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class MessageDetailService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private MessageDetailMapper messageDetailMapper;

    /**
     * 消息明细，查询饲料
     * @param params
     * @return
     */
    public List<FeedMsgDTO> queryFeedMessage(FeedMsgQueryDTO params){
        List<String> feedIdList = params.getFeedIdList();
        if(ObjectUtils.isEmpty(feedIdList)){
            return new ArrayList<>();
        }
        List<FeedMsgDTO> list = messageMapper.queryFeedMessage(feedIdList);
        if(ObjectUtils.isEmpty(list)){
            list =  new ArrayList<>();
        }
        return list;
    }
    public List<FeedMsgDTO> queryFeedMessage(List<String> feedIdList ){
        FeedMsgQueryDTO params = new FeedMsgQueryDTO();
        params.setFeedIdList(feedIdList);
        return queryFeedMessage(params);
    }
    public FeedVO queryFeedVoMessage(List<String> feedIdList ){
        FeedVO feedVO = new FeedVO();
        feedVO.setFeedMsgList(this.queryFeedMessage(feedIdList));
        feedVO.fetchImageNameList();
        return feedVO;
    }

    public FeedVO queryFeedVoMessage(String feedId ){
        List<String> feedIdList = AppUtils.stringToList(feedId);
        FeedVO feedVO = new FeedVO();
        feedVO.setFeedMsgList(this.queryFeedMessage(feedIdList));
        feedVO.fetchImageNameList();
        return feedVO;
    }

    public FeedVO queryFeedVoPondNames(List<QueryPondNamesOutDTO> pondNamesList){
        Set<String> feedIdSet = new HashSet<>();
        if(ObjectUtils.isNotEmpty(pondNamesList)){
            for (QueryPondNamesOutDTO dto : pondNamesList){
                feedIdSet.addAll(dto.getFeedIdSet());
            }
        }
        List<String> feedIdList = new ArrayList<>();
        feedIdList.addAll(feedIdSet);
        return this.queryFeedVoMessage(feedIdList);
    }


    /**
     * 消息明细，塘口名称
     * @param pondIdList
     * @return
     */
    public List<String> queryPondNamesMessage(List<String> pondIdList){
        if(ObjectUtils.isEmpty(pondIdList)){
            return new ArrayList<>();
        }
        pondIdList = AppUtils.stringsDistinct(pondIdList);
        List<String> list = messageMapper.queryPondNamesMessage(pondIdList);
        if(ObjectUtils.isEmpty(list)){
            list = new ArrayList<>();
        }
        return list;
    }

    public List<MessageDetailEntity> queryMessageContentList(String messageId){
        return messageDetailMapper.selectList(new QueryWrapper<MessageDetailEntity>().eq("message_id",messageId));
    }

    public MessageDetailEntity queryMessageContentDetail(String messageId){
        List<MessageDetailEntity> list = this.queryMessageContentList(messageId);
        if(ObjectUtils.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }


}
